#!/usr/bin/env python
# -*- coding: latin-1; py-indent-offset:4 -*-
################################################################################
# 
# This file is part of Bfplusplus
#
# Bfplusplus is a graphical interface to the Betfair Betting Exchange
# Copyright (C) 2010 Daniel Rodriguez (aka Daniel Rodriksson)
# Copyright (C) 2011 Sensible Odds Ltd.
#
# You can learn more and contact the author at:
#
#    http://code.google.com/p/bfplusplus/
#
# Bfplusplus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Bfplusplus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bfplusplus. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""Subclass of PrioritizeEvents, which is generated by wxFormBuilder."""

import wx

import MainGui

# Implementing PrioritizeEvents
class PrioritizeEvents(MainGui.PrioritizeEvents):

    def __init__(self, parent, eventList, prioList):
	MainGui.PrioritizeEvents.__init__(self, parent)

	self.eventList = eventList
	self.prioList = prioList

	for event in self.eventList:
	    if event in self.prioList:
		continue
	    self.m_listBoxEvents.Append(event)

        for prio in self.prioList:
            self.m_listBoxPrio.Append(prio)


    def MoveFromTo(self, src, dst):
	selections = list(src.GetSelections())
	selections.sort(reverse=True)
	for selection in selections:
	    event = src.GetString(selection)
	    src.Delete(selection)
	    dst.Append(event)

	
    def OnButtonClickAdd(self, event):
        self.MoveFromTo(self.m_listBoxEvents, self.m_listBoxPrio)

	    
    def OnButtonClickRemove(self, event):
        self.MoveFromTo(self.m_listBoxPrio, self.m_listBoxEvents)


    def MovePrioEvent(self, direction):
	if direction > 0:
	    limit = self.m_listBoxPrio.GetCount() - 1
	else:
	    limit = 0
	selection = self.m_listBoxPrio.GetSelection()

	if selection == wx.NOT_FOUND or selection == limit:
	    return

	prioEvent = self.m_listBoxPrio.GetString(selection)
	self.m_listBoxPrio.Delete(selection)
	selection += direction
	self.m_listBoxPrio.Insert(prioEvent, selection)
        self.m_listBoxPrio.Select(selection)

	
    def OnButtonClickMoveUp(self, event):
	self.MovePrioEvent(direction=-1)

	
    def OnButtonClickMoveDown(self, event):
	self.MovePrioEvent(direction=1)

	
    def OnOKButtonClick(self, event):
	self.eventList = list()
	count = self.m_listBoxEvents.GetCount()
	for itemIndex in xrange(count):
	    event = self.m_listBoxEvents.GetString(itemIndex)
	    self.eventList.append(event)
	
	self.prioList = list()
	count = self.m_listBoxPrio.GetCount()
	for itemIndex in xrange(count):
	    event = self.m_listBoxPrio.GetString(itemIndex)
	    self.prioList.append(event)

	self.EndModal(wx.ID_OK)
